package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.calendar.CalendarView;
import com.hxkcloud.nurse.utils.BrightnessUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * ProjectName: YJHCuzhong
 * PackageName: com.yjhcloud.stroke.popup
 * Description:带清空按钮的日历选择
 * Create by Ray.
 * Date: 2016/10/11 14:06
 */
public class CalendarClearPopWindow extends PopupWindow {


    private Activity context;
    private CalendarView calendar;
    private TextView clearTV;

    public CalendarClearPopWindow(final Activity context) {
        this.context = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_calendar_clear, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_alpha);

        calendar = (CalendarView) rootView.findViewById(R.id.calendar);
        clearTV = (TextView) rootView.findViewById(R.id.calendar_tv_clear);
        clearTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearListener.clear();
                dismiss();
            }
        });

        //设置日期点击监听器
        calendar.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() {

            @Override
            public void onDateUnselected(Date date) {
            }

            @Override
            public void onDateSelected(Date date) {
                calendarListener.setCalendar((new SimpleDateFormat("yyyy-MM-dd")).format(date));
                dismiss();
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
//                BrightnessUtils.lightOn(context);
            }
        });

//        Calendar cal = Calendar.getInstance();
//        List<Calendar> calendars = new ArrayList<Calendar>();
//        Calendar cal1 = Calendar.getInstance();
//        cal1.set(cal1.get(Calendar.YEAR), 6, 5, 0, 0, 0);
//        calendars.add(cal1);
//        calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
//                false, true, calendars);
    }

    /**
     * 设置标注点
     *
     * @param cals      标注点
     * @param pointType 0普通点1普通点+孕点2孕点
     */
    public void setPoints(List<Calendar> cals, int pointType) {
        if (cals != null && cals.size() > 0) {
//            Calendar cal = Calendar.getInstance();
//            if (pointType == 0) {
//                calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
//                        false, true, cals);
//            } else {
//                calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
//                        true, false, cals);
//            }

            for (Calendar cal : cals) {
                if (pointType == 0) {
                    calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                            false, true, cals);
                } else if (pointType == 2) {
                    calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                            true, false, cals);
                } else {
                    calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                            true, true, cals);
                }
            }
        }

    }

    public void setPoints(List<Calendar> cals1, List<Calendar> cals2) {
        if (cals1 != null && cals1.size() > 0) {
            if (cals2 != null && cals2.size() > 0) {
                cals1.addAll(cals2);
            }
            for (Calendar cal : cals1) {
                calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cals1, cals2);
            }
        }

    }

    public void showDropdown(View parent) {
        clearOldSelections();
        showAsDropDown(parent, 0, 0);
//        BrightnessUtils.lightOff(context);
    }

    public void showBottom(View parent) {
        clearOldSelections();
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    public interface CalendarClearListener {
        void clear();
    }

    public void setClearListener(CalendarClearListener clearListener) {
        this.clearListener = clearListener;
    }

    private CalendarClearListener clearListener;


    public interface CalendarListener {
        void setCalendar(String calendar);
    }

    private CalendarListener calendarListener;

    public void setCalendarListener(CalendarListener calendarListener) {
        this.calendarListener = calendarListener;
    }

    public void clearOldSelections() {
        calendar.clearOldSelections();
    }

}
